<?php
class Racks extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'racks';
	}
	
	public function relations()
	{
		return array(
			'orgRackRelation'=>array(self::HAS_MANY, 'OrgRackRelation', 'rack_ID'),
			'sites'=>array(self::HAS_MANY, 'Sites', 'rack_ID'),
		);
	}
	
	//returns resultset of all states with racks
	public function getAllStates() {
		$sql = "SELECT state FROM ".$this->tableName()." GROUP BY state";
		$states = $this->findAllBySql($sql);
		
		return $states;
	}
	
	//returns resultset of racks for a state
	public function getRacks($state) {
		$sql = "SELECT rack_ID, city FROM ".$this->tableName()." WHERE state='".$state."' ORDER by city";
		$racks = $this->findAllBySql($sql);
		
		return $racks;
	}
	
	public function getRacksInfo()
	{
		$connection = Yii::app()->db;
		$sql = "SELECT * FROM ".$this->tableName()." ORDER BY STATE";
		$command = $connection->createCommand($sql);
		$dataReader = $command->query();
		$res=array();
		foreach($dataReader as $row)
		{
			$key=$row['rack_ID'];
			$res[$key]=$row['state'].'-----'.$row['city'];
		}
		return $res;
	}
	
	public function getRackIDByName($city)
	{
		$sql = "SELECT rack_ID FROM ".$this->tableName()." where city='".$city."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach($resultset as $record)
		{
			$rackID = $record->rack_ID;
		}
		
		return $rackID;
	}
}
?>